// <copyright file="Slider.cs" company="YUIAsp.Net">
// Copyright (c) 2009 All Right Reserved
// </copyright>
// <author>raul popescu</author>
// <email>dev@yuiasp.net</email>
// <date>2009-01-30</date>
// <summary>YUIAsp.Net File</summary>

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using YUIAspNet.Common;

[assembly: WebResource("YUIAspNet.Slider.Slider.js", "text/javascript")]
[assembly: WebResource("YUIAspNet.EmbeddedSkins.DefaultSkin.Slider.slider.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("YUIAspNet.EmbeddedSkins.DefaultSkin.Slider.bgV.gif", "image/gif")]
[assembly: WebResource("YUIAspNet.EmbeddedSkins.DefaultSkin.Slider.bgH.gif", "image/gif")]
[assembly: WebResource("YUIAspNet.EmbeddedSkins.DefaultSkin.Slider.thumbBarV.gif", "image/gif")]
[assembly: WebResource("YUIAspNet.EmbeddedSkins.DefaultSkin.Slider.thumbBarH.gif", "image/gif")]
namespace YUIAspNet
{
    /// <summary>
    /// Slider control.
    /// </summary>
    [ToolboxItem(true)]
    public class Slider : YUIScriptBaseControl, INamingContainer
    {
        Panel pnlSliderBg = null;
        Panel pnlSliderThumb = null;
        Image imgThumbBar = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="Slider"/> class.
        /// </summary>
        public Slider()
        {
            pnlSliderBg = new Panel();
            pnlSliderBg.ID = "sliderBg";
            
            pnlSliderThumb = new Panel();
            pnlSliderThumb.ID = "sliderThumb";
            
            imgThumbBar = new Image();
            imgThumbBar.ID = "thumbBar";

            Init += new EventHandler(Slider_Init);
            PreRender += new EventHandler(Slider_PreRender);

            InitialValue = decimal.Zero;
            MinValue = decimal.Zero;
            MaxValue = 100;
            Orientation = SliderOrientation.Vertical;
            IncrementSize = 1;
            IncrementFactor = 1;
        }

        void Slider_Init(object sender, EventArgs e)
        {
            Controls.Add(pnlSliderBg);
            pnlSliderBg.Controls.Add(pnlSliderThumb);
            pnlSliderThumb.Controls.Add(imgThumbBar);
        }

        void Slider_PreRender(object sender, EventArgs e)
        {
            if (SlideImageUrl==null)
            {
                if (Orientation == SliderOrientation.Vertical)
                    imgThumbBar.ImageUrl = FindSkinImage("Slider", "thumbBarV.gif");
                else
                    imgThumbBar.ImageUrl = FindSkinImage("Slider", "thumbBarH.gif");
            }
            else
                imgThumbBar.ImageUrl = SlideImageUrl.AbsolutePath;

            pnlSliderThumb.CssClass = string.Concat("sliderThumb_", Orientation.ToString());
            pnlSliderBg.CssClass = string.Concat("sliderBg_", Orientation.ToString());
        }

        /// <summary>
        /// Registers the style sheets for the current control.
        /// Must be overriden in the parent control to add any css file reference.
        /// </summary>
        protected override void RegisterStyleSheets()
        {
            RegisterEmbeddedStylesheet("Slider", "slider.css");
        }

        /// <summary>
        /// Registers any additional scripts to include for this control.
        /// </summary>
        protected override void RegisterScriptReferences()
        {
            AddScriptReference(new ScriptReference("YUIAspNet.Slider.Slider.js", GetType().Assembly.FullName));
        }

        /// <summary>
        /// Register the values to send to the client control for initialization.
        /// </summary>
        protected override void RegisterClientInitialization()
        {
            AddProperty("SliderBg", pnlSliderBg.ClientID);
            AddProperty("SliderThumb", pnlSliderThumb.ClientID);
            AddProperty("SliderOrientation", (int)Orientation);
            AddProperty("MinValue", MinValue);
            AddProperty("MaxValue", MaxValue);
            AddProperty("InitialValue", InitialValue);
            AddProperty("IncrementSize", IncrementSize);
            AddProperty("IncrementFactor", IncrementFactor);

            if (!string.IsNullOrEmpty(OnClientChange))
                AddEvent("onChange", OnClientChange);
        }

        #region Public properties
        /// <summary>
        /// Javascript function to execute when slider changes.
        /// </summary>
        public string OnClientChange { get; set; }

        /// <summary>
        /// Get / set initial value.
        /// </summary>
        public decimal InitialValue { get; set; }

        /// <summary>
        /// Get / set maximum value
        /// </summary>
        public decimal MaxValue { get; set; }

        /// <summary>
        /// Get / set minimum value
        /// </summary>
        public decimal MinValue { get; set; }

        /// <summary>
        /// Get / set increment factor
        /// </summary>
        public decimal IncrementFactor { get; set; }

        /// <summary>
        /// Get / set slider orientation
        /// </summary>
        public SliderOrientation Orientation { get; set; }

        /// <summary>
        /// Get / set increment size
        /// </summary>
        public decimal IncrementSize { get; set; }

        /// <summary>
        /// Get / set slide image url.
        /// </summary>
        public Uri SlideImageUrl { get; set; } 
        #endregion
    }
}